﻿CREATE FUNCTION [dbo].[fs_NextState]
(	@StateFromID int, @Direction int -- -1 back, 1 forward, 0 to nearest one
)RETURNS int
AS
BEGIN
RETURN(
SELECT TOP 1 StateToID
FROM         dbo.fs_NextStates(@StateFromID)
WHERE     (Dir * SIGN(@Direction) >= ABS(@Direction)) AND Dir <> 0
ORDER BY ABS(Dir)
)
END 


